package com.cn.web;

import com.cn.event.ReaderFile;
import com.cn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Map;

/**
 * Created by chenyuncong on 2018/11/14.
 */
@RestController
public class TestOracleWeb {
    @Autowired
    private UserService userService;

    Map<String,Integer> map = ReaderFile.map;

    @GetMapping("countTable")
    public String countTable(){
        try{
            File writename = new File("/media/test.txt"); // 相对路径，如果没有则要建立一个新的output。txt文件
            writename.createNewFile(); // 创建新文件
            BufferedWriter out = new BufferedWriter(new FileWriter(writename));
            int num = 0;
            for(String  key: map.keySet()){
                num = userService.count(countSql(key));
                System.out.println(key+" - "+map.get(key)+" - "+num);
                out.write(key+" - "+map.get(key)+" - "+num + "\r\n"); // \r\n即为换行
            }

            out.flush(); // 把缓存区内容压入文件
            out.close(); // 最后记得关闭文件
        }catch (Exception e){

        }finally {

        }

        return "OK";
    }

    private String countSql(String tableName){
        StringBuffer sql = new StringBuffer("select count(1) from mydba2.");
        sql.append(tableName);
        return sql.toString();
    }
}
